package com.example.boot3.service.Impl;

import com.example.boot3.mapper.RepairMapper;
import com.example.boot3.mapper.VehicleMapper;
import com.example.boot3.model.Repair;
import com.example.boot3.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class RepairServiceImpl implements RepairService {
    @Autowired
    RepairMapper repairMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<Repair> getRepairList() {
        return repairMapper.getRepairList();
    }

    @Override
    public Integer applyRepair(Repair repair) {
        /** 1代表空闲可维修, 2代表出车 3代表正在维修 */
        try{
            String status = vehicleMapper.getVehicleStatus(repair.getVehicle_id());
            if(status.equals("出车")){
                return 2;
            }
            else if(status.equals("维修")){
                return 3;
            }
            repairMapper.applyRepair(repair);
            vehicleMapper.updateVehicleStatus(repair.getVehicle_id(),"维修");
            return 1;
        }
        catch (Exception e){
            return 0;
        }
    }

    @Override
    public List<Map<String,Object>> getCostByTime() {
        List<Map<String,Object>> dataList = new ArrayList<>();
        Map<String, Object> data = new HashMap<>();
        data.put("status","本周" );
        data.put("cost", repairMapper.getCostThisWeek());
        Map<String, Object> data1 = new HashMap<>();
        data1.put("status","本月" );
        data1.put("cost", repairMapper.getCostThisMonth());
        Map<String, Object> data2 = new HashMap<>();
        data2.put("status","本年" );
        data2.put("cost", repairMapper.getCostThisYear());
        dataList.add(data);
        dataList.add(data1);
        dataList.add(data2);


        return dataList;
    }
}
